package common

import (
	jwtToken2 "examProject/intelnal/tools/jwt"
	"examProject/pkg/golbal"
	"github.com/dgrijalva/jwt-go"
	"time"
)

/**
@author fuyx
@desc 通过user信息生成token
*/

func CreateToken() string {
	//生成token
	jwtNew := jwtToken2.NewJWT(golbal.ApiConfig.Jwt.Secret)
	tokenClaims := jwtToken2.CustomClaims{
		ID:       1,
		NickName: "1",
		Mobile:   "1",
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + golbal.ApiConfig.Jwt.ExpiresTime, //30天过期
		},
	}
	token, _ := jwtNew.CreateToken(tokenClaims)
	return token
}

/**
@author fuyx
@desc 通过auth协议进行token鉴权解析
*/

func ParseToken(token string) *jwtToken2.CustomClaims {
	jwtNew := jwtToken2.NewJWT(golbal.ApiConfig.Jwt.Secret)
	tokenInfo, err := jwtNew.ParseToken(token)
	if err != nil {
		panic(err)
	}
	return tokenInfo
}
